using System;
using System.Net;
using System.Net.Mail;
using System.ServiceModel;
using DemoLib.Shared.Contracts;

namespace DemoLib.Service.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class EmailService : IEmailService
    {
        public static string DefaultHostname = "mailserver";
        public static int DefaultPort = 25;

        private SmtpClient _client;

        public EmailService()
        {
            Hostname = DefaultHostname;
            Port = DefaultPort;
        }

        public string Hostname { get; set; }
        public int Port { get; set; }

        public void SendEmail(string fromAddress, string toAddress, string subject, string body)
        {
            //NetworkCredential networkCredential = new NetworkCredential(username, password);   
            _client = new SmtpClient(Hostname, Port) { Credentials = CredentialCache.DefaultNetworkCredentials };
            //_client = new SmtpClient(Hostname,25);
            //_client.Credentials = CredentialCache.DefaultNetworkCredentials;

            var message = body.Replace(@"\", "\n");

            var email = new MailMessage(fromAddress, toAddress, subject, message)
                {
                    IsBodyHtml = true
                };

            _client.Send(email);
        }

        /*
         * Yahoo!	smtp.mail.yahoo.com	587	Yes
         * GMail	smtp.gmail.com	587	Yes
         * Hotmail	smtp.live.com	587	Yes
         */
        /*
         * using System.Net;
using System.Net.Mail;

string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;

string emailFrom = "email@yahoo.com";
string password = "abcdefg";
string emailTo = "someone@domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress(emailFrom);
    mail.To.Add(emailTo);
    mail.Subject = subject;
    mail.Body = body;
    mail.IsBodyHtml = true;
    // Can set to false, if you are sending pure text.

    mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
    mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
    {
        smtp.Credentials = new NetworkCredential(emailFrom, password);
        smtp.EnableSsl = enableSSL;
        smtp.Send(mail);
    }
}
         */
    }
}